﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;

namespace App
{
    public partial class EventStatistics : System.Web.UI.Page
    {

        AppLib.Team team;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
                Response.Redirect("Login.aspx");

            int ID = int.Parse(Server.HtmlEncode(Request.Cookies["EventStats"].Value));

            team = MidApp.ManageTeam.GetTeamByID(ID);

            SetChart();

        }

        private void SetChart()
        {

            //typ grafu
            Chart.Series["Series1"].ChartType = SeriesChartType.Column;
            //vzdalenost sloupcu
            Chart.Series["Series1"]["PointWidth"] = "0.85";
            //typ sloupcu
            Chart.Series["Series1"]["DrawingStyle"] = "Cylinder";
            //2D nebo 3D graf
            Chart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;

            //ziskani vsech clenu tymu
            var totalTeamMembers = MidApp.ManageIsMember.GetAllMembers(team.TeamID);

            //ziskani vsech vypsanych akci pro tym
            var eventTotalCount = MidApp.ManagePossession.GetAllEventsAssignedToTeam(team.TeamID).Count;
            
            //nastaveni osy Y
            Chart.ChartAreas["ChartArea1"].AxisY.Maximum = eventTotalCount;
            Chart.ChartAreas["ChartArea1"].AxisY.Interval = eventTotalCount / 10;

            //cuklus vytvori kazdemu clenu tymu sloupec
            for (int i = 0; i < totalTeamMembers.Count; i++)
            {
                int totalSigned = MidApp.ManageParticipate.GetUserParticipationInTeam(team.TeamID, totalTeamMembers[i].UserID).Count;
                double y = ((double)totalSigned / (double)eventTotalCount) * 100;

                
                DataPoint dp = new DataPoint(i + 1, totalSigned);
                dp.AxisLabel = totalTeamMembers[i].User.Nick;
                
                //nastaveni barvz popisku, pod 70 cervena, nad zelena
                if (y >= 70)
                    dp.LabelForeColor = Color.LawnGreen;
                else
                    if (y >= 60)
                        dp.LabelForeColor = Color.DarkOrange;
                    else
                        dp.LabelForeColor = Color.Red;

                dp.XValue = Chart.Series["Series1"].Points.Count + 1;
                dp.IsValueShownAsLabel = false;
                dp.Label = String.Format("{0:0.#}%", y);
                dp.ToolTip = totalTeamMembers[i].User.Nick + " se zúčastnil " + String.Format("{0:0.#}", y) + "% vypsaných akcí.";

                Chart.Series["Series1"].Points.Add(dp);
            }

        }


    }
}